package com.breaktian.demoapp.ui.refreshdemo;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.breaktian.assemble.recyclerview.ListBaseAdapter;
import com.breaktian.assemble.recyclerview.SuperViewHolder;
import com.breaktian.assemble.refresh.superrefresh.PullRefreshLayout;
import com.breaktian.assemble.utils.ThreadUtil;
import com.breaktian.demoapp.R;

import java.util.ArrayList;
import java.util.List;

public class RecyclerActivity extends AppCompatActivity {

    private RecyclerView recyclerview;
    private PullRefreshLayout pullRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler);
        pullRefreshLayout = (PullRefreshLayout)findViewById(R.id.pullRefreshLayout);
        pullRefreshLayout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onPullDown() {
                ThreadUtil.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        pullRefreshLayout.stopPullDownRefresh();
                    }
                },1500);

            }

            @Override
            public void onLoadMore() {
                ThreadUtil.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        pullRefreshLayout.stopLoadMore();
                    }
                },1500);

            }
        });
        recyclerview = (RecyclerView)findViewById(R.id.recyclerview);
        List list = new ArrayList<String>();
        for(int i=0;i<40;i++){
            list.add("hello word "+i);
        }

        MyAdapter myAdapter = new MyAdapter(this);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerview.setLayoutManager(linearLayoutManager);
        recyclerview.setAdapter(myAdapter);

        myAdapter.setDataList(list);
    }


    public class MyAdapter extends ListBaseAdapter<String>{



        public MyAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_recycler;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView textView = (TextView)holder.getView(R.id.tv_text);
            textView.setText(getDataList().get(position));
        }
    }

}
